package com.ruixin.mvc.config;

import com.ruixin.annotation.BeanScan;
import com.ruixin.base.exception.BaseException;
import com.ruixin.base.factory.Factory;
import com.ruixin.base.util.BeanUtil;
import com.ruixin.base.util.StringUtil;
import com.ruixin.config.ApplicationContext;
import com.ruixin.config.BeanConstant;
import com.ruixin.config.Environment;
import com.ruixin.server.TomcatServer;
import com.sun.istack.internal.NotNull;

/**
 * 上下文
 */
public class ApplicationContextWrapper extends ApplicationContext {

    public ApplicationContextWrapper(Environment environment) throws BaseException {
        super(environment);
    }

    //Web工厂
    private Factory<Class,WebMapping> webMappingFactory = new Factory<>();

    //注入
    private WebRegister webRegister = new WebRegister();

    /**
     * 通过配置类加载
     * @param clz
     * @return
     * @throws BaseException
     */
    public static ApplicationContextWrapper builder(@NotNull Class<?> clz) throws BaseException {
        BeanScan beanScan = BeanUtil.getAnnotation(clz, BeanScan.class);
        if(beanScan!=null){
            String[] beanPackage = beanScan.beanPackage();
            String configFile = beanScan.configFile();
            String packs = StringUtil.join(beanPackage, ",");
            EnvironmentWrapper environment = new EnvironmentWrapper(configFile,packs);
            ApplicationContextWrapper applicationContext = new ApplicationContextWrapper(environment);
            applicationContext.load();
            return applicationContext;
        }else {
            throw new BaseException(clz.getName()+"类中找不到BeanScan类型注解");
        }
    }

    /**
     * 配置文件加载
     * @param configFile
     * @return
     * @throws BaseException
     */
    public static ApplicationContextWrapper builder(String configFile) throws BaseException {
        EnvironmentWrapper environment = new EnvironmentWrapper(configFile);
        ApplicationContextWrapper applicationContext = new ApplicationContextWrapper(environment);
        applicationContext.load();
        return applicationContext;
    }

    @Override
    public EnvironmentWrapper getEnvironment() {
        return (EnvironmentWrapper) super.getEnvironment();
    }

    public Factory<Class, WebMapping> getWebMappingFactory() {
        return webMappingFactory;
    }

    public void setWebMappingFactory(Factory<Class, WebMapping> webMappingFactory) {
        this.webMappingFactory = webMappingFactory;
    }

    public void setWebMapping(Class<?> clz,WebMapping webMapping){
        this.webMappingFactory.put(clz,webMapping);
    }

    public WebMapping getWebMapping(Class<?> clz){
        return this.webMappingFactory.get(clz);
    }

    @Override
    protected void loading() throws BaseException {
        super.loading();
        TomcatServer tomcatServer = new TomcatServer();
        WebConfiguration webConfiguration = getWebConfiguration();
        //静态资源处理
        webConfiguration.addStaticMapping(webRegister);
        tomcatServer.start(this);
    }

    public String getContextPath(){
        return this.getEnvironment().contextPath();
    }

    /**
     * 获取webConfiguration配置
     * @return
     */
    public WebConfiguration getWebConfiguration(){
        WebConfiguration webConfiguration = null;
        if(this.getBeanFactory().containsKey(BeanConstant.NAME_WebConfiguration)){
            webConfiguration = (WebConfiguration) this.getBean(BeanConstant.NAME_WebConfiguration);
        }else{
            webConfiguration = new WebConfiguration();
        }
        return webConfiguration;
    }

    public WebRegister getWebRegister() {
        return webRegister;
    }

    public void setWebRegister(WebRegister webRegister) {
        this.webRegister = webRegister;
    }
}
